package com.aivin.lib_reader.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.SeekBar;

import com.aivin.lib_reader.R;
import com.aivin.lib_reader.icallback.MenuLayCallBack;


/**
 * 底部弹窗
 */
public class ReaderBottomLay extends FrameLayout {

    private boolean isShowing = true;

    public ReaderBottomLay(Context context) {
        this(context, null);
    }

    public ReaderBottomLay(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ReaderBottomLay(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.reader_menu_layout, this);
        setClickable(true);
        initView() ;
    }

    private SeekBar mChapterSeekBar ;
    private void initView(){
        mChapterSeekBar = findViewById(R.id.chapter_seek_bar);
        mChapterSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if(menuLayCallBack!=null){
                    menuLayCallBack.onChangeChapterSeekBar(seekBar.getProgress());
                }
            }
        });


        // 初始化SeekBar位置
        mChapterSeekBar.setProgress(0);// 如果需要历史纪录的话，可以在这里实现
        findViewById(R.id.setting).setOnClickListener(onClickListener);//设置
        findViewById(R.id.text_prev_chapter).setOnClickListener(onClickListener);//上一章
        findViewById(R.id.text_next_chapter).setOnClickListener(onClickListener);//下一章
        findViewById(R.id.reader_catalogue).setOnClickListener(onClickListener);//目录
    }



    public void updateChapterSeekbar(boolean is2up ){
        if(is2up){
            mChapterSeekBar.setProgress(mChapterSeekBar.getProgress() + 1);
        }else{
            mChapterSeekBar.setProgress(mChapterSeekBar.getProgress() - 1);
        }
    }


    public void setChapterSeekbar(int progress ){
        mChapterSeekBar.setMax(progress);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = v.getId();//上一章
//目录
            if (i == R.id.setting) {
                dismiss();
                if (menuLayCallBack != null) {
                    menuLayCallBack.onOpenSettingView(true);
                }
            } else if (i == R.id.text_prev_chapter) {
                if (menuLayCallBack != null) {
                    menuLayCallBack.on2PreChapter();
                }
            } else if (i == R.id.text_next_chapter) {
                if (menuLayCallBack != null) {
                    menuLayCallBack.on2NextChapter();
                }
            } else if (i == R.id.reader_catalogue) {
                if (menuLayCallBack != null) {
                    menuLayCallBack.onOpenCatalogue(true);
                }
            }
        }
    };


    private MenuLayCallBack menuLayCallBack ;

    public void setMenuLayCallBack(MenuLayCallBack menuLayCallBack) {
        this.menuLayCallBack = menuLayCallBack;
    }


    public void show() {
        setVisibility(View.VISIBLE);
        isShowing =true ;
    }



    public void dismiss() {
        setVisibility(View.GONE);
        isShowing =false ;
    }

    public boolean isShowing() {
        return isShowing;
    }

}
